const routerResponse = (option = {}) => {
  return async (ctx, next) => {
    ctx.success = (data, msg, type) => {
      ctx.type = type || option.type || 'json'
      ctx.body = {
        state: 1,
        code: option.successCode || '200',
        msg: msg || option.successMsg || 'success',
        data
      }
    }
    ctx.fail = (msg, code) => {
      ctx.type = option.type || 'json'
      ctx.body = {
        state: 0,
        code: option.failCode || code || '000001',
        msg: msg || option.failMsg || 'fail'
      }
    }
    await next()
  }
}

module.exports = routerResponse
